#pragma once

#include "Log.hpp"
#include "nocopy.hpp"
#include"InetAddr.hpp"
#include <iostream>
#include <type_traits>// IWYU pragma: keep
#include <unistd.h>
#include<functional>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <string>

using namespace log_ns;
using namespace std;

static const int gsockfd = -1;
static const uint16_t glocalport = 8899;
enum
{
    SOCKET_ERROR = 1,
    BIND_ERROR
};

using service_t =function<void(int,string message,InetAddr &who)>;

//一般服务器主要是用来进行网络数据读取和写入的，IO的
//服务器IO逻辑 和 业务逻辑 解耦
class UdpServer : public nocopy
{
public:
    UdpServer(service_t func,uint16_t localport = glocalport)
        : _func(func),
          _sockfd(gsockfd),
          _localport(localport),
          _isrunning(false)
    {
        
    }

    void InitServer()
    {
        // 1.创建socket文件
        _sockfd = ::socket(AF_INET, SOCK_DGRAM, 0);
        if (_sockfd < 0)
        {
            LOG(FATAL, "socker error\n");
            exit(SOCKET_ERROR);
        }
        LOG(DEBUG, "socket create success, _sockfd:%d\n", _sockfd);

        // 2.bind绑定
        struct sockaddr_in local;
        memset(&local, 0, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(_localport);
        //local.sin_addr.s_addr = inet_addr(_localip.c_str()); // a.需要4字节IP b.需要网络序列的IP -- 暂时
        local.sin_addr.s_addr=INADDR_ANY;//服务器端进行任意IP地址绑定

        int n = ::bind(_sockfd, (struct sockaddr *)&local, sizeof(local));
        if (n < 0)
        {
            LOG(FATAL, "bind error\n");
            exit(BIND_ERROR);
        }
        LOG(DEBUG, "socket bind success\n");
    }

    void Start()
    {
        _isrunning=true;
        char message[1024];
        while(_isrunning)
        {
            struct sockaddr_in peer;
            socklen_t len=sizeof(peer);
            ssize_t n=recvfrom(_sockfd,message,sizeof(message)-1,0,(struct sockaddr*)&peer,&len);
            if(n>0)
            {
                // uint16_t peerport = ntohs(peer.sin_port);
                // string peerip = inet_ntoa(peer.sin_addr);

                InetAddr addr(peer);


                message[n]=0;
                LOG(DEBUG,"[%s]# %s\n",addr.AddrStr().c_str(),message);
                _func(_sockfd,message,addr);
                LOG(DEBUG,"return udpserver\n");
            }
            else
            {
                cout<<"recvfrom, error"<<endl;
            }
        }
        _isrunning=false;
    }
    ~UdpServer()
    {
        if(_sockfd>gsockfd)::close(_sockfd);
    }

private:
    int _sockfd;//读写都用同一个sockfd，说明：UDP是全双工通信的！
    uint16_t _localport;
    bool _isrunning;

    service_t _func;
};